﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Yuecai.Crypto
{
    /// <summary>
    /// 实现加密解密操作的类
    /// </summary>
    public class DesCrypto
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="key">密码</param>
        /// <returns>密文</returns>
        public static byte[] Encrypt(string plainText, string key)
        {
            byte[] k = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] iv = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] p = Encoding.Default.GetBytes(plainText);
            return new TripleDESCryptoServiceProvider().CreateEncryptor(k, iv).TransformFinalBlock(p, 0, p.Length);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="key">密码</param>
        /// <returns>明文</returns>
        public static string Decrypt(byte[] cipherText, string key)
        {
            byte[] k = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] iv = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(key));
            return Encoding.Default.GetString(
                new TripleDESCryptoServiceProvider().CreateDecryptor(k, iv).TransformFinalBlock(cipherText, 0,
                                                                                                cipherText.Length));
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="key">密码</param>
        /// <returns>密文</returns>
        public static byte[] EncryptBytes(byte[] plainText, string key)
        {
            byte[] k = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] iv = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] p = plainText;
            return new TripleDESCryptoServiceProvider().CreateEncryptor(k, iv).TransformFinalBlock(p, 0, p.Length);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="key">密码</param>
        /// <returns>明文</returns>
        public static byte[] DecryptBytes(byte[] cipherText, string key)
        {
            byte[] k = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(key));
            byte[] iv = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(key));
            return new TripleDESCryptoServiceProvider().CreateDecryptor(k, iv).TransformFinalBlock(cipherText, 0,
                                                                                                cipherText.Length);
        }
    }
}
